package com.jixi.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jixi.mapper.OrderDailyMapper;
import com.jixi.mapper.StudentMapper;
import com.jixi.pojo.entity.OrderDaily;
import com.jixi.pojo.entity.Student;
import com.jixi.service.OrderDailyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

@Service
public class OrderDailyServiceImpl extends ServiceImpl<OrderDailyMapper, OrderDaily> implements OrderDailyService {
    @Autowired
    private OrderDailyMapper orderDailyMapper;
    @Autowired
    private StudentMapper studentMapper;
    /**
     * 根据日期查询今日订餐
     * */
    @Override
    public Long getByDateCount() {
        //获取今日日期
        // 创建一个Date对象（当前日期和时间）
        java.util.Date date = new Date();
        // 创建一个SimpleDateFormat对象，指定日期格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        // 使用SimpleDateFormat的format方法将Date对象转换为String
        String formattedDate = sdf.format(date);
        //获取当前班主任id
        Long teacherId = StpUtil.getLoginIdAsLong();
        Long orderCount = orderDailyMapper.getByOrderCount(formattedDate,teacherId);
        return orderCount;
    }
    /**
     * 根据日期查询今日退餐
     * */
    @Override
    public Long getByReturnCount() {
        //获取今日日期
        // 创建一个Date对象（当前日期和时间）
        java.util.Date date = new Date();
        // 创建一个SimpleDateFormat对象，指定日期格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        // 使用SimpleDateFormat的format方法将Date对象转换为String
        String formattedDate = sdf.format(date);
        //获取当前班主任id
        Long teacherId = StpUtil.getLoginIdAsLong();
        Long returnCount = orderDailyMapper.getByReturnCount(formattedDate,teacherId);
        return returnCount;
    }
    /**
     * 查询累计订餐天数
     * */
    @Override
    public Long getOrderDays() {
        //获取当前班主任id
        Long teacherId = StpUtil.getLoginIdAsLong();
        Long oderDays = orderDailyMapper.getOderDays(teacherId);
        return oderDays;
    }
    /**
     * 累计本月订餐人数
     * */
    @Override
    public Long getOrderCounts() {
// 创建一个Date对象（当前日期和时间）
        Date date = new Date();
        // 创建一个SimpleDateFormat对象，指定日期格式为 "yyyy-MM"
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM", Locale.getDefault());
        // 使用SimpleDateFormat的format方法将Date对象转换为String
        String formattedDate = sdf.format(date);
        //获取当前班主任id
        Long teacherId = StpUtil.getLoginIdAsLong();
        Long orderCounts = orderDailyMapper.getOrderCounts(formattedDate,teacherId);
        return orderCounts;
    }
    /**
     * 已完成定餐人数
     * */
    @Override
    public Long getHaveOrder(Long classId,String date) {
        LambdaQueryWrapper<OrderDaily> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(OrderDaily::getClassId, classId)
                     .in(OrderDaily::getOrderDateStatus, 0,1)
                     .eq(OrderDaily::getOrderDate,date);
        Long haveOrder = orderDailyMapper.selectCount(queryWrapper1);
        return haveOrder;
    }
    /**
     * 已退餐人数
     * */
    @Override
    public Long getReturnOrder(Long classId, String date) {
        LambdaQueryWrapper<OrderDaily> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.eq(OrderDaily::getClassId, classId)
                .in(OrderDaily::getOrderDateStatus, 2,3,4)
                .eq(OrderDaily::getOrderDate,date);
        Long returnOrder = orderDailyMapper.selectCount(queryWrapper2);
        return returnOrder;
    }
    /**
     * 未完成定餐人数
     * */
    @Override
    public Long getNoOrder(Long classId) {
        LambdaQueryWrapper<Student> queryWrapper3 = new LambdaQueryWrapper<>();
        queryWrapper3.eq(Student::getClassId, classId);
        Long totalNum = studentMapper.selectCount(queryWrapper3);
        return totalNum;
    }
}
